<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends Backend {

	public function __construct()
	{
		parent::__construct();
		$this->lang->load('sites');
		$this->load->model(['site_model','sitecategory_model','siteproduct_model','siteorder_model','sitedisapproved_model']);
		$this->data['page_home']  = lang('lang_dashboard');
		$this->data['page_title'] = lang('lang_site');
		$this->data['sitetype'] = $this->config->item('site_types');

	}


	public function index($siteid)
	{

		$site=$this->site_model->getitembyid($siteid);
		if(!$site) die('error link');

		$this->data['nav']=$this->get_site_nav($site,1);
	
		$this->data['checkapi']=$this->mysite->checklink;

		$this->data['site']=$site;
		// Title
		$this->data['meta_title'] = $this->data['page_title'];

		// Breadcrumbs
		$this->data['array_breadcrumbs'] = array(
			$this->data['page_home']  => 'backend',
			$this->data['page_title'] => 'backend/sites/site',
			'站点分类'=>'backend/sites/category'
		);

		if(IS_POST && $this->input->post("action"))
        {
            if($this->input->post("action")=="add")
            {
                $newdata=[
                    "name"=>$this->input->post("name"),
                    "pid"=>$this->input->post("pid"),
                    "sort"=>$this->input->post("sort"),
					"siteid"=>$siteid,
                ];
                $this->sitecategory_model->create($newdata);
                redirect('backend/sites/category/index/'.$siteid, 'refresh');
                die();
            }
		}

		$page_data=$this->sitecategory_model->getcat($siteid);
		if($page_data):
		foreach($page_data as &$item)
		{
			$item["gcount"]=$this->siteproduct_model->getcatproductcount($siteid,$item["rid"]);
			if($item["sub"])
			{
				foreach($item["sub"] as &$sub)
				{
					$sub["gcount"]=$this->siteproduct_model->getcatproductcount($siteid,$sub["rid"]);
				}
			}
		}
		endif;
		$this->data['data']=$page_data;
		
		$root = $this->sitecategory_model->getroot($siteid);
		$this->data['parent']=$root;

		$this->data['content_header_title'] = content_header_title($this->data['page_title'],$site['name'].' 分类');
		$this->data['content'] = 'sites/category';

		$this->render();
	}

	public function ajaxget($siteid)
	{
		$cat=$this->sitecategory_model->getcat($siteid);
		foreach($cat as $k=>$v)
		{
			if($v["rid"]<=0)
			{
				unset($cat[$k]);
			}
			else
			{	
				if($v["sub"]):
				foreach($v["sub"] as $j=>$u)
				{
					if($u["rid"]<=0)
					{
						unset($cat[$k][$j]);
					}
				}
				endif;
			}
		}
		$result=json_encode($cat);
		die($result);
	}


	public function sync($siteid)
	{
		$site=$this->site_model->getitembyid($siteid);
		if(!$site) die('error link');
		$returndata=["status"=>0,"msg"=>""];

		loadsystemlink($site);

		if($this->mysite->checklink)
		{
			$cats=$this->sitecategory_model->getcat($siteid);
			$returninfo=$this->mysite->synccate($cats);
			if($returninfo["data"]){
				foreach($returninfo["data"] as $k=>$v)
				{
					$this->sitecategory_model->update_insert($v,$siteid);
				}
			}
            $returndata["status"]=1;
		}
		else
		{
			$returndata["msg"]="接口连接错误，请检查";
		}
		echo json_encode($returndata,JSON_UNESCAPED_UNICODE);
		die();
	}


    public function ajaxedit()
    {
        $attrName=$this->input->post("attrName");
		$newHtml=$this->input->post("newHtml");
		if(!in_array($attrName,['name','sort'])) die();
        $cid=$this->input->post("cid");
        $newdata[$attrName]=$newHtml;
        $this->sitecategory_model->update($cid, $newdata);
        echo json_encode([]);
        die();
	}

	public function ajaxdel()
	{

		$return=["status"=>1];
		$cid=$this->input->post("cid");
		

		$cat=$this->sitecategory_model->getitembyid($cid);
		if(!$cat)
		{
			$return["status"]=0;
			$return["msg"]="记录不存在";			
		}
		else
		{

			$child=$this->sitecategory_model->getchildren($cid);
			if($child)
			{
				$return["status"]=0;
				$return["msg"]="包含下级分类，请勿删除";	
			}
			else
			{

				if($cat["rid"])
				{
					$site=$this->site_model->getitembyid($cat["siteid"]);
					loadsystemlink($site);

					if(!$this->mysite->checklink)
					{
						$return["status"]=0;
						$return["msg"]="请检查接口配置";	
					}
					else
					{
						if(!$this->sitecategory_model->delitem($cid))
						{
							$return["status"]=0;
							$return["msg"]="删除失败";
						}
						else
						{
							//$this->mysite->delcatgory($cat);
						}
					}
				}
				else
				{
					$this->sitecategory_model->delitem($cid);
				}
			}

		}
        echo json_encode($return);
		die();
		
	}
	
}
